#!/bin/bash
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test if Plugin Objects are deleted corectly by 'delete object' command.
#
TestName="console-delete-object-test"
JobName=pluginTest
. scripts/functions

scripts/cleanup
scripts/copy-plugin-confs
make -C $src/src/plugins/fd install install-test-plugin
if [ $? -ne 0 ]; then
    print_debug "Failed to build and install test-plugin!"
    exit 1
fi

file=encrypt-bug.jpg
rm -rf ${cwd}/tmp/*
echo "${cwd}/README" >${cwd}/tmp/file-list

start_test

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=File1 volume=TestVolume001
run job=TestPluginTest storage=File1 yes
wait
status client=$CLIENT
messages
@$out ${cwd}/tmp/log2.out
list objects
quit
END_OF_DATA

run_bacula

vm_objects_lines=`cat ${cwd}/tmp/log2.out | grep 'VMWare' | wc -l`
if [ ${vm_objects_lines} -eq 0 ]; then
   estat=1
   print_debug "Did not find any object of 'VMWare' type"
fi

db_objects_lines=`cat ${cwd}/tmp/log2.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -eq 0 ]; then
   estat=1
   print_debug "Did not find any object of 'PostgreSQL' type"
fi

cat $tmp/log2.out | awk  '/VMWare/ {print $2 }' > $tmp/vmware_ids.log
readarray -t vmware_ids < $tmp/vmware_ids.log

cat $tmp/log2.out | awk  '/PostgreSQL/ {print $2 }' > $tmp/postgresql_ids.log
readarray -t postgresql_ids < $tmp/postgresql_ids.log

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log3.out
delete
7
${vmware_ids[0]}
@$out ${cwd}/tmp/log4.out
list objects
quit
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log4.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne 1 ]; then
   estat=1
   print_debug "Wrong VMWare plugin objects count found in output: ${objects_lines}"
fi

objects_lines=`cat ${cwd}/tmp/log4.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne $db_objects_lines ]; then
   estat=1
   print_debug "No PostgreSQL object should be deleted now"
fi

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log5.out
delete object objectid="${postgresql_ids[0]},${postgresql_ids[1]}"
@$out ${cwd}/tmp/log6.out
list objects
quit
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log6.out | grep 'VMWare' | wc -l`
if [ ${objects_lines} -ne 1 ]; then
   estat=1
   print_debug "Wrong VMWare plugin objects count found in output: ${objects_lines}"
fi

db_objs_expected=`expr ${db_objects_lines} - 2`
objects_lines=`cat ${cwd}/tmp/log6.out | grep 'PostgreSQL' | wc -l`
if [ ${objects_lines} -ne ${db_objs_expected} ]; then
   estat=1
   print_debug "Wrong PostgreSQL object cound found: ${objects_lines}, expected: ${db_objs_expected}"
fi

# Purge objects and run job once more to have all objects generated by the test plugin back in db
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log7.out
purge volume=TestVolume001
@$out ${cwd}/tmp/log8.out
run job=TestPluginTest storage=File1 yes
wait
messages
@$out ${cwd}/tmp/log9.out
list objects
quit
END_OF_DATA

run_bconsole

# Delete objects by 'category' filter
db_objects=`cat ${cwd}/tmp/log9.out | grep 'Database' | wc -l`
vm_objects=`cat ${cwd}/tmp/log9.out | grep 'Virtual Machine' | wc -l`

cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log10.out
delete object category=Database
yes
@$out ${cwd}/tmp/log11.out
list objects
@$out ${cwd}/tmp/log12.out
delete object category="Virtual Machine"
yes
@$out ${cwd}/tmp/log13.out
list objects
quit
END_OF_DATA

run_bconsole

objects_lines=`cat ${cwd}/tmp/log11.out | grep 'Database' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "Not all VMWare objects deleted with 'Database' filter. Objects left: ${objects_lines}"
fi

objects_lines=`cat ${cwd}/tmp/log13.out | grep 'Virtual Machine' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "Not all VMWare objects deleted with 'Virtual Machine' filter. Objects left: ${objects_lines}"
fi

# Run job once more to have all objects generated by the test plugin back in db
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log14.out
run job=TestPluginTest storage=File1 yes
wait
messages
@$out ${cwd}/tmp/log15.out
llist objects
quit
END_OF_DATA

run_bconsole

db_objects=`cat ${cwd}/tmp/log15.out | grep 'objectcategory: Database' | wc -l`
obj_name_everything=`cat ${cwd}/tmp/log15.out | grep 'objectname: everything' | wc -l`

# Try to use two filters at once
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@$out ${cwd}/tmp/log16.out
delete object category=Database uuid=1234-abc-testplugin
@$out ${cwd}/tmp/log17.out
llist objects
@$out ${cwd}/tmp/log18.out
delete object category=Database name=everything
yes
@$out ${cwd}/tmp/log19.out
llist objects
quit
END_OF_DATA

run_bconsole

# No db obj should be deleted (there is no db with '1234-abc-testplugin' uuid
objects_lines=`cat ${cwd}/tmp/log17.out | grep 'objectcategory: Database' | wc -l`
if [ ${objects_lines} -ne ${db_objects} ]; then
   estat=1
   print_debug "Database objects should not be deleted at all, expected: ${db_objects}, have: ${objects_lines}"
   exit 1
fi

# 2 'everything' objs should be deleted
objects_lines=`cat ${cwd}/tmp/log19.out | grep 'objectname: everything' | wc -l`
if [ ${objects_lines} -ne 0 ]; then
   estat=1
   print_debug "Not all 'everything' objects deleted with 'name' and 'category' filters. Objects left: ${objects_lines}"
fi


stop_bacula
end_test
